package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackSkinDao;
import com.haipumi.utils.Encrypt;

public class BackSkinDaoImpl implements IBackSkinDao {

	DBHelper db=new DBHelper();
	@Override
	public List<haipumi_skin> findAllSkin() {
		String sql=" select * from haipumi_skin ";
		List<haipumi_skin>skinlist=null;
		try {
			skinlist=db.finds(sql, null, haipumi_skin.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return skinlist; 
	}

	@Override
	public int countAllSkin() {
		String sql="select count(*) from haipumi_skin ";
		double r=0;
		try {
			r=db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int)r;
	}

	@Override
	public haipumi_skin insertSkin(haipumi_skin skin) {
		List<Object>params=new ArrayList<Object>();
		String sql=null;
		sql=" insert into haipumi_skin (simage,scolor,sstatus) "+
				"values('lk_select_jd_bg4.jpg',?,?)";
		haipumi_skin newskin=null;
		//params.add(skin.getSimage());
		params.add(skin.getScolor());
		params.add(skin.getSstatus());
		int result=0;
		try {
			result=db.insertWithKey(sql, params);
			if(result>0){
				//成功
				newskin=findSkin(result);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return newskin;
	}

	private haipumi_skin findSkin(int sid) {
		String sql=null;
		sql=" select * from haipumi_skin where sid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(sid);
		haipumi_skin newskin=null;
		try {
			List<haipumi_skin>newskinlist=db.finds(sql, params, haipumi_skin.class);
			newskin=newskinlist.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newskin;
	}

	@Override
	public haipumi_skin updateSkin(haipumi_skin skin) {
		boolean flag=false;
		String sql;
		sql=" update haipumi_skin set simage=?,scolor=?,sstatus=? where sid=? ";
		List<Object>params=new ArrayList<Object>();
		haipumi_skin newskin=null;
		params.add(skin.getSimage());
		params.add(skin.getScolor());
		params.add(skin.getSstatus());
		params.add(skin.getSid());
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//成功了，将现在的user返回
				newskin=skin;
			}else{
				newskin=findSkin(Integer.parseInt(skin.getSid()));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newskin;
	}

}
